<template>
  <div>
    <van-nav-bar title="面经登录" />
    <van-form @submit="onSubmit">
      <van-field
        :rules="[{ required: true, message: '请填写用户名' }]"
        label="用户名"
        label-width="50"
        name="username"
        placeholder="用户名"
        v-model="username"
      />
      <van-field
        :rules="[{ required: true, message: '请填写密码' }]"
        label="密码"
        label-width="50"
        name="password"
        placeholder="密码"
        type="password"
        v-model="password"
      />
      <div style="margin: 16px;">
        <van-button block native-type="submit">登录</van-button>
      </div>
      <div style="text-align: right;padding-right:20px;">
        <router-link style="color:#0970a5" to="/register">注册账号</router-link>
      </div>
    </van-form>
  </div>
</template>
<script>
import { setToken } from '@/utils/storage'
import { Notify } from 'vant';
import { loginAPI } from '@/api/user.js'
// import { Toast } from 'vant';
import request from '@/utils/request.js'
export default {
  data () {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values);
      // const res = await request.post('/user/login', values)
      const res = await loginAPI(values)
      setToken(res.data.token)
      Notify({ type: 'success', message: '登录成功' });
      this.$router.push('/')
    },
  },
};
</script>
<style lang='less'  scoped>
.van-button {
  background-color: #fa6d1d;
  color: #fff;
}
</style>